#!/usr/bin/env python
# encoding: utf-8
"""
@summary: 计算日活跃用户数;reducer需要拆分value
@attention: 1.使用正则作为输入；2.在程序中用hash结构
@author: hongxingfan
@since: 2014年8月12日    上午9:58:23
"""
import sys

# 日期和pv的实体，用于放置在list中
class Entity:
    def __init__(self, date, pv):
        self.date = date
        self.pv = pv
        
    def __str__(self):
        print("%d:%d" % (self.date, self.pv))

if __name__ == "__main__":
    
    hash_users = {}
    
    for line in sys.stdin:
        line = line.strip()
        
        cols = line.split("\t")
        
        suv = cols[0]
        values = cols[1]
        
        # 组装成实体放到list中
        entity_str = values.split(";")  # reduer过来的value值
        list_value = []  #
        last_index = len(entity_str) - 1
        for (index, s) in enumerate(entity_str):
            # 抛弃最后一个
            if index == last_index:
                break
            date_pv = s.split(":")
            entity = Entity(date_pv[0], int(date_pv[1]))
            list_value.append(entity)
        
        # 判断suv是否已经在hash_users中
        if suv in hash_users:
            # 遍历查找list中的日期是否等于当前日期
            list = hash_users.get(suv)
            last_index = len(list) - 1
            for l_v in list_value:
                for (index, element) in enumerate(list):
                    if element.date == l_v.date:
                        element.pv += l_v.pv
                        break
                    if index == last_index:
                        list.append(l_v)
                        break
                    
        else:
            hash_users[suv] = list_value
            
    # 打印hash_users，hash_users的value是list
    for (k, v) in hash_users.items():
        values = "{"
        for entity in v:
            values += entity.date + ":" + str(entity.pv) + ";"
        values += "}"
        print("%s\t%s" % (k, values))
